function Show-StatWelcomeMessage
{ 
    Write-Host "Michael Nigels (http://www.AllThingsMicrosoft.com)"
    Write-Host "PowerShell Automation Tools for SharePoint v$versionText"
    Write-Host
    Write-Host "Welcome to the SharePoint Total Automation Toolkit" 
    Write-Host "=========================================================================================" 
    Write-Host
    Write-Host "GETTING STARTED"
    Write-Host "     This toolkit provides a set of step-by-step orchestration scripts which can be used"
    Write-Host "     for all stages of SharePoint installation, site provisioning, and custom solution"
    Write-Host "     deployment. Those scripts are numbered and grouped into folders representing each"
    Write-Host "     stage of deployment. In addition, this toolkit provides a complete set of granular"
    Write-Host "     installation and site provisioning functions which can be called directly and in"
    Write-Host "     any order. To see a complete list of available STAT functions type " -NoNewline
    Write-Host "Get-STATFunctions " -ForegroundColor Yellow
    Write-Host "     at the command prompt."
    Write-Host
    Write-Host "     To return to this menu at any time type " -NoNewline
    Write-Host "Show-StatMenu " -ForegroundColor Yellow -NoNewline
    Write-Host "     at the command prompt."
    Write-Host
    Write-Host "ADDITIONAL HELP"
    Write-Host "     All scripts in this toolkit make use of the PowerShell help system. To get additional"
    Write-Host "     information about any script or function in this toolkit use the " -NoNewline
    Write-Host "Get-Help " -NoNewline
    Write-Host "command"
    Write-Host "     Example (for functions): " -NoNewline
    Write-Host "Get-Help New-StatSPMetadataServiceApplication " -ForegroundColor Yellow
    Write-Host "     Example (for .ps1 scripts): " -NoNewline
    Write-Host "Get-Help .\17-CreateInitialSiteTopology.ps1 " -ForegroundColor Yellow
    Write-Host 
} 

function Show-StatMenu([boolean] $showWelcomeMessage = $true)
{
    if ($showWelcomeMessage -eq $true)
    {
        cls
        Show-StatWelcomeMessage
    }

    $prompt =  "     Where would you like to begin? `n`n"
    $prompt += "     SharePoint Server`n"
    $prompt += "     [ 1] Install Software `n"
    $prompt += "     [ 2] Prepare a Server Template `n"
    $prompt += "     [ 3] Configure SharePoint Farm and Service Apps `n"
    $prompt += "     [ 4] Deploy Sites, Custom Solutions, and Content `n"
    $prompt += "     [ 5] Conduct a Full Site Topology Redeploy `n"
    $prompt += "     [ 6] Conduct a Full Site Topology Redeploy (Preserving Web Applications) `n`n"
    $prompt += "     Office Web Application Server `n"
    $prompt += "     [ 7] Install Software `n"
    $prompt += "     [ 8] Prepare a Server Template `n"
    $prompt += "     [ 9] Configure WAC Server Farm `n`n"
    $prompt += "     Worflow Server `n"
    $prompt += "     [10] Install Software `n"
    $prompt += "     [11] Prepare a Server Template `n"
    $prompt += "     [12] Configure Service Bus & Workflow Manager Farms `n`n"
    $prompt += "     General `n"
    $prompt += "     [13] Command Prompt `n`n"
    $prompt += "     Please select a number (1-13)"

    switch (Read-Host $prompt)
    {
        1
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\01 Install Software"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        2
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\02 Create Sysprep'd Template"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        3
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\03 Config Farm and Service Apps"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        4
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\04 Setup Sites"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        5
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\05 Utility Scripts and Tools"
            . "$STATHome\Step-By-Step\SharePoint\05 Utility Scripts and Tools\FullSiteTopologyRedeploy.ps1" -ReprovisionManagedMetaDataApp
        }

        6
        {
            Set-Location "$STATHome\Step-By-Step\SharePoint\05 Utility Scripts and Tools"
            . "$STATHome\Step-By-Step\SharePoint\05 Utility Scripts and Tools\FullSiteTopologyRedeploy.ps1" -ReprovisionManagedMetaDataApp -PreserveWebApps
        }

        7
        {
            Set-Location "$STATHome\Step-By-Step\WacServer\01 Install Software"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        8
        {
            Set-Location "$STATHome\Step-By-Step\WacServer\02 Create Sysprep'd Template"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        9
        {
            Set-Location "$STATHome\Step-By-Step\WacServer\03 Config Farm"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }

        10
        {
            Set-Location "$STATHome\Step-By-Step\Workflow\01 Install Software"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }
        
        11
        {
            Set-Location "$STATHome\Step-By-Step\Workflow\02 Create Sysprep'd Template"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }

        12
        {
            Set-Location "$STATHome\Step-By-Step\Workflow\03 Config Farm"
            dir | Format-Table @{Expression={$_.Name};Label="Available Scripts"}
        }

        default
        {
            Write-Host
            Set-Location "$STATHome\Step-By-Step"
        }
    }
} 

function prompt
{ 

   Write-Host "STAT " -NoNewline
   Write-Host (pwd).Path.Replace($STATHome, '~') -NoNewline
   Write-Host '>' -NoNewline
   Return ' ' 
} 

function Test-ElevatedProcess
{
    <#
        .SYNOPSIS
           Tests to see whether the current session has been elevated to "Run As Administrator..."

        .DESCRIPTION
           This function tests to see whether the current session has been elevated to "Run As Administrator..." and returns $true if the process is elevated and $false if it is not.

        .EXAMPLE
           Test-ElevatedProcess
    #>

    $CurrentWindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $CurrentWindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($CurrentWindowsIdentity)
    
    [bool] $IsProcessElevated = $CurrentWindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)

    return $IsProcessElevated;
}

function Get-StatFunctions
{
    Get-ChildItem function:* | Where-Object {$_.Module -Match "SPTotalAutomationToolkit"} | Format-Table Name
}


#
# Main Script
#

CLS
$STATHome = "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit"

Set-Location "$STATHome\Step-By-Step"

$versionFile = "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit\Modules\Version.csv"
$versions = Import-Csv $versionFile
if ($versions -ne $null)
{
	foreach ($version in $versions)
	{
        $versionText = $version.Major + "." + $version.Minor + "." + $version.Build + "." + $version.Revision
	}
}

# Display the Welcome Message
Show-StatWelcomeMessage 

# Test to see if the process was "Run as administrator..."
$processIsElevated = Test-ElevatedProcess

if ($processIsElevated -eq $false)
{
    Write-Warning "The SharePoint Total Automation Toolkit Shell requires elevated permissions. `n         Please close this window and select 'Run as administrator' when launching these tools."
    Write-Host

    $prompt =  "     Press 'Y' to continue without administrator permissions. `n"
    $prompt += "     Press any other key to close this window"

    switch (Read-Host $prompt)
    {
        'Y'
        {
            Write-Host
            Write-Host "Continuing without administrator permissions..." -ForegroundColor Red
            Write-Host
        }
        default
        {
            Stop-Process $PID
        }
    }
}

# Load the SharePoint Snap-In for PowerShell if it is available
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Import the Office Web Apps Server Module if it is available
Import-Module OfficeWebApps -ErrorAction SilentlyContinue

# Import the Server Manager Module if it is available
Import-Module "$env:windir\System32\WindowsPowerShell\v1.0\Modules\ServerManager" -ErrorAction SilentlyContinue

# Import the SharePoint Total Automation Toolkit Modules
Import-Module "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit\Modules\SPTotalAutomationToolkit.Common"
Import-Module "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit\Modules\SPTotalAutomationToolkit.SoftwareInstall"
Import-Module "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit\Modules\SPTotalAutomationToolkit.FarmConfig"
Import-Module "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit\Modules\SPTotalAutomationToolkit.SiteConfig"

# Display the STAT Menu
Show-StatMenu $false